<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="内容"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="点赞数" prop="likesNum">
      <el-input v-model="dataForm.likesNum" placeholder="点赞数"></el-input>
    </el-form-item>
    <el-form-item label="创建用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="创建用户id"></el-input>
    </el-form-item>
    <el-form-item label="是否匿名发布" prop="anonymous">
      <el-input v-model="dataForm.anonymous" placeholder="是否匿名发布"></el-input>
    </el-form-item>
    <el-form-item label="阅读量" prop="reading">
      <el-input v-model="dataForm.reading" placeholder="阅读量"></el-input>
    </el-form-item>
    <el-form-item label="举报次数" prop="reports">
      <el-input v-model="dataForm.reports" placeholder="举报次数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          content: '',
          createtime: '',
          likesNum: '',
          userId: '',
          anonymous: '',
          reading: '',
          reports: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          likesNum: [
            { required: true, message: '点赞数不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '创建用户id不能为空', trigger: 'blur' }
          ],
          anonymous: [
            { required: true, message: '是否匿名发布不能为空', trigger: 'blur' }
          ],
          reading: [
            { required: true, message: '阅读量不能为空', trigger: 'blur' }
          ],
          reports: [
            { required: true, message: '举报次数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/main/article/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.article.title
                this.dataForm.content = data.article.content
                this.dataForm.createtime = data.article.createtime
                this.dataForm.likesNum = data.article.likesNum
                this.dataForm.userId = data.article.userId
                this.dataForm.anonymous = data.article.anonymous
                this.dataForm.reading = data.article.reading
                this.dataForm.reports = data.article.reports
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/main/article/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'createtime': this.dataForm.createtime,
                'likesNum': this.dataForm.likesNum,
                'userId': this.dataForm.userId,
                'anonymous': this.dataForm.anonymous,
                'reading': this.dataForm.reading,
                'reports': this.dataForm.reports
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
